﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.LiveFX.Client;

namespace LiveFrameworkExplorer.Plugins.LiveItemEditor
{
    public partial class ucLiveItemEditor : UserControl
    {
        private LiveItem _item;
        private ILFEBase _lfeBase = null;

        public ucLiveItemEditor()
        {
            InitializeComponent();
        }

        public void SetObject<TMeshItem, TResource>(ILFEBase lfeBase, object item)
            where TMeshItem : Microsoft.LiveFX.Client.LiveItem<TResource>, new()
            where TResource : Microsoft.LiveFX.ResourceModel.Resource, new()
        {
            _lfeBase = lfeBase;
            _item = item as TMeshItem;
            _item.UpdateCompleted += (object sender, AsyncCompletedEventArgs e) =>
            {
                if (e.Error == null)
                {
                    _lfeBase.GetDTEEnvironment().StatusBar.Progress(false, "Live item updated", 2, 2);
                    //if (MessageBox.Show("Live item updated !\n\nDo you want to reload MOB ?", "Live Mesh Editor",
                    //    MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    //{
                        _lfeBase.Refresh<TMeshItem, TResource>();
                        //_lfeBase.ReloadLFE();
                    //}
                }
                else
                {
                    _lfeBase.GetDTEEnvironment().StatusBar.Progress(false, "Error to update the live item", 2, 2);
                    MessageBox.Show("Error: " + e.Error.Message, "Live Mesh Editor",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            };
            pgDetail.SelectedObject = _item.Resource as TResource;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            _lfeBase.GetDTEEnvironment().StatusBar.Progress(true, "Updating the live item....", 1, 2);
            _item.UpdateAsync(null);
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            _item.Load();
        }
    }
}
